package developer.zyk.ability.chat.ability.memory.msgHandler;

import developer.zyk.ability.chat.dao.entity.ChatHistoryDetailDO;
import developer.zyk.global.common.consts.ChatRole;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.Message;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

/**
 * @author zhangyongkang
 * @time 2025/5/12 16:24
 * @description
 */
@Component
public class MessageAdapter {
    @Resource
    private Map<ChatRole, MessageHandler> messageConverter;


    public Message detail2Message(ChatHistoryDetailDO chatHistoryDetailDO) {
        ChatRole chatRole = ChatRole.queryByRoleCode(chatHistoryDetailDO.getChatRole());
        MessageHandler messageHandler = messageConverter.get(chatRole);
        assert Objects.nonNull(messageHandler);
        return messageHandler.handle(chatHistoryDetailDO);
    }

    public void handleAddMsg(String msgType, Long chatId, Message message) {
        ChatRole chatRole = ChatRole.queryByRoleCode(msgType);
        MessageHandler messageHandler = messageConverter.get(chatRole);
        assert Objects.nonNull(messageHandler);
        messageHandler.add(message, chatId);
    }
}
